package com.it.travel.service.impl;

import com.it.travel.service.EmailService;
import com.it.travel.vo.Params.ContactParams;
import com.it.travel.vo.Params.RegisterParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Service
public class EmailServiceImpl implements EmailService {
    @Autowired
    JavaMailSenderImpl mailSender;

    //        @Async
    @Async   // 因为发送邮件这个操作，是非常耗时的
    public void sendEmail(RegisterParams registerParams) throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        mailSender.setHost("smtp.qq.com");
//        mailSender.setJavaMailProperties(true);
        mailSender.setUsername("596922417@qq.com");
        mailSender.setPassword("qcvdbtysnxmmbfaj");
//        组装
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        //        正文
        helper.setSubject("有点甜旅游网账号激活");
        helper.setText("<p style='color:red;'> 许嵩测试</p> <br>   请点击此链接进行账号的激活"
                        + "http://localhost:8888/user/active?username=" + registerParams.getUsername()
                , true);

//            附件
//        helper.addAttachment("login.jpg", new File("D:\\login.jpg"));
        helper.setTo(registerParams.getEmail());  // 发送给
        helper.setFrom("596922417@qq.com");
        mailSender.send(mimeMessage);
    }


    @Async   // 因为发送邮件这个操作，是非常耗时的
    public void sendEmailForContact(ContactParams contactParams) throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        mailSender.setHost("smtp.qq.com");
//        mailSender.setJavaMailProperties(true);
        mailSender.setUsername("596922417@qq.com");
        mailSender.setPassword("qcvdbtysnxmmbfaj");
//        组装
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        //        正文
        helper.setSubject("有点甜旅游网潜在人才");
        helper.setText("<p style='color:red;'> 有点甜旅游网CEO</p> <br>   请点击此链接进行账号的激活"
                        + contactParams.getName() + "<br>" + contactParams.getAge() + "<br>"
                        + contactParams.getMobile() + "<br>" + contactParams.getDesc()
                , true);

//            附件
//        helper.addAttachment("login.jpg", new File("D:\\login.jpg"));
        helper.setTo("596922417@qq.com");  // 发送给
        helper.setFrom("596922417@qq.com");
        mailSender.send(mimeMessage);
    }
}
